package com.crazyhat.crazyhat.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.crazyhat.crazyhat.attribute.ClubStatusEnum;
import com.crazyhat.crazyhat.entity.Club;
import com.crazyhat.crazyhat.mapper.ClubMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazyhat.crazyhat.attribute.TokenPayloadEnum;
import com.crazyhat.crazyhat.entity.Favorite;
import com.crazyhat.crazyhat.mapper.FavoriteMapper;
import com.crazyhat.crazyhat.service.IFavoriteService;
import com.crazyhat.crazyhat.utils.ThreadLocalUtil;

/**
 * <p>
 * 收藏的社团 服务实现类
 * </p>
 *
 * @author crazyhat
 * @since 2024-07-05
 */
@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements IFavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private ClubMapper clubMapper;

    public Page<Favorite> getPersonalFavorite(int pageNum, int pageSize) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer studentId = (Integer) map.get("studentId");

        QueryWrapper<Club> clubQueryWrapper = new QueryWrapper<>();
        clubQueryWrapper.eq("club_status", ClubStatusEnum.Open)
                .or()
                .eq("club_status",ClubStatusEnum.RestrictJoining);

        List<Club> openClubs = clubMapper.selectList(clubQueryWrapper);
        List<Integer> openClubIds = openClubs.stream().map(Club::getClubId).collect(Collectors.toList());

        if (openClubIds.isEmpty()) {
            return new Page<>(pageNum, pageSize);
        }

        QueryWrapper<Favorite> favoriteQueryWrapper = new QueryWrapper<>();
        favoriteQueryWrapper.eq("student_id", studentId)
                .in("club_id", openClubIds)
                .orderByDesc("favorited_time");

        Page<Favorite> page = new Page<>(pageNum, pageSize);

        return favoriteMapper.selectPage(page, favoriteQueryWrapper);
    }

    @Override
    public boolean getClubFavoriteState(Integer studentId,Integer clubId) {

        // Map<String,Object> map=ThreadLocalUtil.get();
        // Integer studentId=(Integer)map.get("studentId");

        if (studentId==null||clubId==null) {
            return false;
        }

        QueryWrapper<Favorite> queryWrapper=new QueryWrapper<>();

        queryWrapper
            .eq("student_id", studentId)
            .eq("club_id", clubId);

            if (favoriteMapper.selectOne(queryWrapper)==null) {
                return false;
            } else {
                return true;
            }
    }

    @Override
    public void favoriteNewClub(Integer clubId) {

        Map<String,Object> map=ThreadLocalUtil.get();
        Integer studentId=(Integer)map.get(TokenPayloadEnum.StudentId.getValue());

        if (getClubFavoriteState(studentId,clubId)) {
            return;
        }

        /**
         * 创建新收藏记录
         */
        Favorite newFavorite=new Favorite();
        newFavorite.setClubId(clubId);
        newFavorite.setStudentId(studentId);
        newFavorite.setFavoritedTime(LocalDateTime.now());

        favoriteMapper.insert(newFavorite);
    }


    /**
     * 取消收藏
     */
    @Override
    public boolean cancelFavorite(Integer clubId) {

        Map<String,Object> map=ThreadLocalUtil.get();
        Integer studentId=(Integer)map.get(TokenPayloadEnum.StudentId.getValue());

        QueryWrapper<Favorite> queryWrapper=new QueryWrapper<>();

        queryWrapper
            .eq("student_id", studentId)
            .eq("club_id", clubId);

        return favoriteMapper.delete(queryWrapper)>0;
    }

}
